package com.example.mapper;

import com.example.pojo.Road;
import org.apache.ibatis.annotations.Mapper;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//读取文件中的内容
@Mapper
public interface GetAllRoad {

    //时间字典
     Map<String, Integer> timeDictionary = new HashMap<>();
    public static void readDataFromFile(String filePath) {
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split("\\s+"); // 假设数据是以空格分隔的

                // 获取起点、终点和时间
                String start = parts[0];
                String end = parts[1];
                int time = Integer.parseInt(parts[2]);

                // 构建字典的键
                String key = start + " to " + end;

                // 将时间存入字典
                timeDictionary.put(key, time);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static int getTime(String start, String end) {
        // 从字典中获取时间
        String key = start + " to " + end;
        return timeDictionary.getOrDefault(key, -1);
    }

    /**
     * 获取路网信息
     * @returnS
     */

    public static List<Road> getAll() {
        readDataFromFile("src/main/resources/static/bus_times.txt");
        List<Road> road = new ArrayList<>();
        List<Road> list = new ArrayList<>();
        String filename = "src/main/resources/static/bus1.txt";
        list = parseBusTicketsFromFile(filename, 1);
        road.addAll(list);
        filename = "src/main/resources/static/train1.txt";
        list = parseBusTicketsFromFile(filename, 2);
        road.addAll(list);
        filename = "src/main/resources/static/fly1.txt";
        list = parseBusTicketsFromFile(filename, 3);
        road.addAll(list);
        return road;
    }

    /*public static void main(String[] args) {
        System.out.println(getAll());
    }
*/
    public static List<Road> parseBusTicketsFromFile(String filename, Integer types) {
        List<Road> list = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-ddHH:mm:ss");
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] fields = line.split(" ");
                String deptCity = getField(fields[0]);
                String arrCity = getField(fields[1]);
                LocalDateTime start = LocalDateTime.parse(getField(fields[2]) + fields[3], formatter);
                LocalDateTime end = null;
                try {
                    end = LocalDateTime.parse(getField(fields[4]+fields[5]), formatter);
                } catch (Exception e) {
                    int time = getTime(deptCity, arrCity);
                    if(time!=-1){
                        end=start.plusSeconds(time);
                    }
                }
                Double seatPrice = Double.parseDouble(getField(fields[6]));
                list.add(new Road(deptCity, arrCity, start, end, types, seatPrice));
            }
            return list;


        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static String getField(String field) {
        return field.substring(field.indexOf("=") + 1);
    }




}
